package org.easy.db;

import lombok.Getter;

import java.util.Arrays;

/**
 * @Description: 数据库类型
 * @Author: wxl
 * @CreateTime: 2022/10/27 17:41
 */
@Getter
public enum Type implements Sql{

    /**
     *
     */
    MYSQL("MYSQL",
            "com.mysql.cj.jdbc.Driver",
            "jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"
    ),
    ORACLE("ORACLE",
            "oracle.jdbc.driver.OracleDriver",
            "jdbc:oracle:thin:@%s:%s%s"),

    SQL_SERVER("SQL_SERVER",
            "com.microsoft.sqlserver.jdbc.SQLServerDriver",
            "jdbc:sqlserver://%s:%s;DatabaseName=%s"),

    POSTGRE_SQL("POSTGRE_SQL",
            "org.postgresql.Driver",
            "jdbc:postgresql://%s:%s/%s"),

    DM("DM",
            "dm.jdbc.driver.DmDriver",
            "jdbc:dm://%s:%s/%s"),

    MONGODB("MONGODB",
            "",
            ""),
    CK("CK",
            "com.clickhouse.jdbc.ClickHouseDriver",
            "jdbc:clickhouse://%s:%d/%s");

    private final String type;

    private final String driver;

    private final String dbUrl;

    private static final String MYSQL_TYPE = "MYSQL";

    private static final String ORACLE_TYPE = "ORACLE";

    private static final String SQL_SERVER_TYPE = "SQL_SERVER";

    private static final String POSTGRE_SQL_TYPE = "POSTGRE_SQL";

    private static final String DM_TYPE = "DM";

    private static final String MONGODB_TYPE = "MONGODB";

    private static final String CK_TYPE = "CK";

    Type(String type,String driver,String dbUrl){
        this.type = type;
        this.driver = driver;
        this.dbUrl = dbUrl;
    }

    public static Type getType (String type) {
        return Arrays.stream(Type.values())
                .filter(t -> t.getType().equals(type))
                .findAny()
                .orElseThrow(() -> new NullPointerException("没有找到对应的类型"));
    }

    public  String getUrlByType (String url,int port,String dataBase) {
        return String.format(this.getDbUrl(),url,port,dataBase);
    }

    /**
     * 根据数据库获取查找表名的SQL
     *
     * @param dataBase 数据库
     * @return String
     */
    @Override
    public String tableNameSql(String dataBase) {
        String sql = null;
        if (MYSQL_TYPE.equals(getType())) {
            sql = Mysql.TABLE_NAME_SQL;
        } else if (ORACLE_TYPE.equals(getType())) {
            sql = Oracle.TABLE_NAME_SQL;
        } else if (SQL_SERVER_TYPE.equals(getType())) {
            sql = SqlServer.TABLE_NAME_SQL;
        } else if (POSTGRE_SQL_TYPE.equals(getType())) {
            sql = PostgreSql.TABLE_NAME_SQL;
        } else if (DM_TYPE.equals(getType())) {
            sql = Dm.TABLE_NAME_SQL;
        } else if (MONGODB_TYPE.equals(getType())) {
            sql = MongoDb.TABLE_NAME_SQL;
        } else if (CK_TYPE.equals(getType())) {
            sql = Ck.TABLE_NAME_SQL;
        }
        if (null != sql) {
            return String.format(sql,dataBase);
        }
        return "";
    }

    /**
     * 根据表名获取表字段查询SQL
     *
     * @param tableName 表名
     * @return String
     */
    @Override
    public String tableColumnSql(String tableName) {
        String sql = null;
        if (MYSQL_TYPE.equals(getType())) {
            sql = Mysql.TABLE_COLUMN_SQL;
        } else if (ORACLE_TYPE.equals(getType())) {
            sql = Oracle.TABLE_COLUMN_SQL;
        } else if (SQL_SERVER_TYPE.equals(getType())) {
            sql = SqlServer.TABLE_COLUMN_SQL;
        } else if (POSTGRE_SQL_TYPE.equals(getType())) {
            sql = PostgreSql.TABLE_COLUMN_SQL;
        } else if (DM_TYPE.equals(getType())) {
            sql = Dm.TABLE_COLUMN_SQL;
        } else if (MONGODB_TYPE.equals(getType())) {
            sql = MongoDb.TABLE_COLUMN_SQL;
        } else if (CK_TYPE.equals(getType())) {
            sql = Ck.TABLE_COLUMN_SQL;
        }
        if (null != sql) {
            return String.format(sql,tableName);
        }
        return "";
    }
}
